// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    banner: [{
      'image': 'https://mp.weixin.qq.com/mpres/htmledition/images//bg/bg_login_banner_v560f36b.jpg'
    }, {
      'image': 'https://mp.weixin.qq.com/mpres/htmledition/images//bg/bg_login_banner_v560f36b.jpg'
    }],
    notice: [{
      'id': 1,
      'subject': '测试公告'
    }, {
      'id': 1,
      'subject': '水熊缓步公告'
    }],
    userInfo: '',
    domain: app.globalData.domain,
  },
  toNextPage: function (a) {
    if (app.globalData.userInfo == null) {
      app.showTip('请先授权登录');
      setTimeout(function () {
        wx.switchTab({
          url: '/pages/my/my',
        })
      }, 2000)
      return;
    }
    var type = a.currentTarget.dataset.type
    var url = ''
    switch (type) {
      case 'rich':
        url = "/pages/rich/rich?id=" + a.currentTarget.dataset.id;
        break;
      case 'ticket':
        url = "/pages/ticket/ticket";
        break;
      case 'register':
        url = "/pages/register/register";
        break;
      case 'tenant':
        url = "/pages/tenant/tenant";
        break;
      case 'buy':
        url = "/pages/buy/buy";
        break;
      case 'contract':
        url = "/pages/contract/contract";
        break;
      case 'notice':
        url = "/pages/notice/notice";
        break;
      case 'repair':
        url = "/pages/repair/repair";
        break;
      case 'huanti':
        url = "/pages/huanti/huanti";
        break;
      case 'teach':
        url = "/pages/teach/teach";
        break;
      case 'qrcode':
        url = "/pages/qrcode/qrcode";
        break;
      case 'cardAndFace':
        url = "/pages/cardAndFace/cardAndFace";
        break;
      case 'deductionRecord':
        url = "/pages/deductionRecord/deductionRecord";
        break;
    }
    if (url == '') {
      app.showTip('非法点击');
      return;
    }
    wx.navigateTo({
      url: url
    });
  },
  onLoad() {
    this.getBanner()
  },
  onShow() {
    if (app.globalData.userInfo != null) {
      this.setData({
        'userInfo': app.globalData.userInfo
      })
    } else {
      var that = this
      app.user_login(that)
    }
  },
  auth: function () {
    var that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        app.getUserInfo(res, that)
      }
    })
  },
  onGetPhoneNumber(e) {
    console.log(e)
    var that = this;
    app.getMobile(e, that)
  },
  //获取基础信息
  getBanner() {
    var that = this
    app.call.getData('index/getWxappIndex', function (data) {
      that.setData({
        banner: data.data.banner,
        notice: data.data.notice
      })
    }, function () { });
  },
  onGetPhoneNumber(e) {
    var that = this;
    app.getMobile(e, that)
  },
  openCustomer() {
    var siteinfo = require("../../siteinfo.js")
    var contact_url = siteinfo.contact_url
    if (this.data.userInfo.hasOwnProperty('contact_url') && (this.data.userInfo.contact_url != '' || this.data.userInfo.contact_url != null)) {
      contact_url = this.data.userInfo.contact_url
    }
    var corpId = siteinfo.corpId
    if (this.data.userInfo.hasOwnProperty('corpId') && (this.data.userInfo.corpId != '' || this.data.userInfo.corpId != null)) {
      var v = this.data.userInfo.corpId
    }
    wx.openCustomerServiceChat({
      extInfo: {
        url: contact_url
      },
      corpId: corpId,
      success(res) { },
      fail(res) {
        app.showTip('打开微信客服失败')
      }
    })
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: '4000850319' //仅为示例，并非真实的电话号码
    })
  }
})